//
//  GMArtBrowserViewController.m
//  gentleman
//
//  Created by 范月成 on 2017/12/8.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMArtBrowserViewController.h"
#import "GMArtworkItem.h"
#import "GMPhotoGroupView.h"

@interface GMArtBrowserViewController ()

@property (nonatomic, strong) UIActivityIndicatorView *indicator;
@property (nonatomic, strong) GMPhotoGroupView *photoGroupView;
@property (nonatomic, strong) UISlider *slider;

@end

@implementation GMArtBrowserViewController

+ (void)load {
    [MGJRouter registerURLPattern:kRouterArtworkBrowser
                        toHandler:^(NSDictionary *routerParameters) {
                            NSDictionary *info = routerParameters[MGJRouterParameterUserInfo];
                            GMArtBrowserViewController *browser = [[GMArtBrowserViewController alloc] init];
                            browser.artwork = info[@"artwork"];
                            browser.gallery = info[@"gallery"];
                            
                            [[UIViewController currentNavigationViewController] pushViewController:browser animated:YES];
                        }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    @weakify(self)
    [GMArtworkItem getArtworksWithUrl:self.artwork.url gid:self.artwork.gid showkey:self.gallery.showkey totalPage:self.artwork.filecount completed:^(NSMutableArray *artworks) {
        @strongify(self)
        
        [self.indicator stopAnimating];
        
        self.photoGroupView = [[GMPhotoGroupView alloc] initWithGroupItems:[artworks copy]];
        self.photoGroupView.tapBlock = ^{
            @strongify(self)
            CGFloat top = self.navigationController.navigationBar.top;
            if (top == self.safeAreaTop-44) {
                [self hiddenNavigationBar];
            } else {
                [self showNavigationBar];
            }
        };
        self.photoGroupView.scrollBlock = ^(NSInteger currentPage) {
            @strongify(self)
            self.slider.value = currentPage;
        };
        [self.photoGroupView addToContainer:self.view];
        [self.view addSubview:self.slider];
    }];
    
    [[SDImageCache sharedImageCache].config setShouldDecompressImages:NO];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
}

- (void)dealloc {
    [[SDImageCache sharedImageCache].config setShouldDecompressImages:YES];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    CGFloat top = self.navigationController.navigationBar.top;
    if (top == - self.safeAreaTop) {
        [self showNavigationBar];
    }
}

- (void)initSubviews {
    [super initSubviews];
    [self.indicator startAnimating];
    [self.view addSubview:self.indicator];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
    }];
    
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
    }];
}

- (void)showNavigationBar {
    [UIView animateWithDuration:0.25f animations:^{
        self.navigationController.navigationBar.top = self.safeAreaTop-44;
    }];
}

- (void)hiddenNavigationBar {
    [UIView animateWithDuration:0.25f animations:^{
        self.navigationController.navigationBar.top = -self.safeAreaTop;
    }];
}

#pragma mark - action
- (void)changeToPage:(UISlider *)sender {
    NSInteger page = (NSInteger)sender.value;
    NSLog(@"%ld", page);
    [self.photoGroupView scrollToPage:page];
}

#pragma mark - getter
- (UIActivityIndicatorView *)indicator {
    if (!_indicator) {
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicator.hidesWhenStopped = YES;
        [_indicator startAnimating];
    }
    return _indicator;
}

- (UISlider *)slider {
    if (!_slider) {
        _slider = [[UISlider alloc] initWithFrame:CGRectMake(20, kScreenHeight-80, kScreenWidth-40, 20)];
        _slider.minimumValue = 0;
        _slider.continuous = NO;
        _slider.maximumValue = self.artwork.filecount;
        _slider.value = 0;
        [_slider addTarget:self action:@selector(changeToPage:) forControlEvents:UIControlEventValueChanged];
    }
    return _slider;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
